package com.lihui.queue;

import java.util.List;

/**
 * 功能描述
 *  单向链表实现队列
 * @author wileda
 * @date 2022/10/18  21:59
 */
public class MyQueue1 {
    public static class ListNode{
        int val;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    int size;
    ListNode first;
    ListNode last;

    public void offer(int e){
        ListNode node = new ListNode(e);
        if (first == null){
            first = node;
        }else {
            last.next = node;
            node.next = null;
        }
        last = node;
        size++;
    }

    public int poll(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        int firstValue = first.val;
        if(first == null){
            last = null;
        }else {
            first = first.next;
        }
        size--;
        return firstValue;
    }

    public int peek(){
        if(isEmpty()){
            throw new RuntimeException("队列为空");
        }
        return first.val;
    }

    public int size(){
        return size;
    }
    private boolean isEmpty() {
        return size == 0;
    }
    public void display(){
        StringBuilder  sb = new StringBuilder();
        if (first == null){
            sb.append("[]");
        }
      ListNode current = first;
        sb.append("[");
        while(current != null){
            sb.append(current.val);
            if(current.next != null){
                sb.append(",");
            }
            current = current.next;
        }
        sb.append("]");
        System.out.println(sb);
    }
}
